package com.yaoming.umengshare;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMVideo;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.media.UMusic;
import com.umeng.socialize.utils.Log;
import com.yaoming.umengshare.adapter.SharePanelAdapter;
import com.yaoming.umengshare.listener.ShareResultListener;

import java.util.List;


 public class ShareActivity extends Activity {
    private boolean hadShow=false;
    private PopupWindow popupWindow;
    private SharePanelAdapter adapter;
    private List<SharePlatform> list;
     private int imageId=-1;
     private String title,text,targetUrl,videoUrl,videoThumb,imageUrl,musicUrl,musicThumb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.share_layout);
        initData();
        initPopWindow();
    }
     private void initData(){
         Intent intent=getIntent();
         list=(List<SharePlatform>)intent.getSerializableExtra("platform_list");
         title=intent.getStringExtra("title");
         text=intent.getStringExtra("text");
         imageUrl=intent.getStringExtra("imageUrl");
         imageId=intent.getIntExtra("imageId",-1);
         targetUrl=intent.getStringExtra("targetUrl");
         videoUrl=intent.getStringExtra("videoUrl");
         musicUrl=intent.getStringExtra("musicUrl");
         musicThumb=intent.getStringExtra("musicThumb");
         videoThumb=intent.getStringExtra("videoThumb");
     }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (!hadShow){
        if (hasFocus){
           showPop();
        }
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();

        if (popupWindow.isShowing()){
            popupWindow.dismiss();
        }
    }




    private void initPopWindow() {

        View popView =  LayoutInflater.from(this).inflate(R.layout.popuwindw_layout, null);
        popupWindow = new PopupWindow(popView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//        popupWindow.setBackgroundDrawable(new ColorDrawable(0));
        //设置popwindow出现和消失动画
        popupWindow.setAnimationStyle(R.style.popuwindowAnimation);
        GridView share_gv=(GridView)popView.findViewById(R.id.share_gv);
//        popView.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (popupWindow!=null&&popupWindow.isShowing()){
//                    popupWindow.dismiss();
//                }
//            }
//        });
        if (UmShareConfig.isChangeNumColumns()){
            share_gv.setNumColumns(UmShareConfig.getNumColumns());
        }

        if (UmShareConfig.isChangBgColor()){
            share_gv.setBackgroundColor(UmShareConfig.getBackgroundColor());
        }

        adapter=new SharePanelAdapter(this,list);
        share_gv.setAdapter(adapter);
        share_gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (UmShare.mContext==null||!(UmShare.mContext instanceof Activity)){
                    return;
                }
                if (((SharePlatform) adapter.getItem(position)).getMedia()==SHARE_MEDIA.WEIXIN_CIRCLE) {
                    if (title!=null&&text!=null){
                        title=title+"["+text+"]";

                    }
                }
                ShareAction action=  new ShareAction((Activity) UmShare.mContext).setPlatform(((SharePlatform)adapter.getItem(position)).getMedia()).setCallback(umShareListener);
                if (text!=null){
                    action.withText(text);
                }

                if (targetUrl!=null){
                    UMWeb mWeb=new UMWeb(targetUrl);
                    if (title!=null)
                        mWeb.setTitle(title);
                    if (text!=null)
                        mWeb.setDescription(text);
                    if (imageUrl!=null){
                        UMImage thumb=new UMImage(ShareActivity.this,imageUrl);
                        thumb.compressFormat= Bitmap.CompressFormat.PNG;
                        mWeb.setThumb(thumb);
                    }else if (imageId!=-1){
                        UMImage thumb=new UMImage(ShareActivity.this,imageId);
                        thumb.compressFormat= Bitmap.CompressFormat.PNG;
                        mWeb.setThumb(thumb);
                    }
                    if(((SharePlatform) adapter.getItem(position)).getMedia()==SHARE_MEDIA.QQ
                           ||((SharePlatform) adapter.getItem(position)).getMedia()==SHARE_MEDIA.QZONE){
                       if (targetUrl.startsWith("http://")||targetUrl.startsWith("https://")){
                           action.withMedia(mWeb);
                       }
                   }else {
                        action.withMedia(mWeb);
                    }

                }else if (videoUrl!=null) {
                    UMVideo video = new UMVideo(videoUrl);
                    if (title!=null)
                        video.setTitle(title);
                    if (text!=null)
                        video.setDescription(text);
                    UMImage thumb=null;
                    if (videoThumb != null) {
                         thumb=new UMImage(ShareActivity.this,videoThumb);
                    }else if (imageUrl!=null){
                         thumb=new UMImage(ShareActivity.this,imageUrl);
                    }else if (imageId!=-1){
                         thumb=new UMImage(ShareActivity.this,imageId);
                    }
                    if (thumb!=null){
                        thumb.compressFormat= Bitmap.CompressFormat.PNG;
                        video.setThumb(thumb);
                    }
                    action.withMedia(video);
                }else if (musicUrl!=null){
                    UMusic music=new UMusic(musicUrl);
                    if (musicThumb!=null){
                        music.setThumb(new UMImage(ShareActivity.this,musicThumb));
                    }
                    action.withMedia(music);
                }else  if (imageUrl!=null){
                    action.withMedia(new UMImage(ShareActivity.this,imageUrl));
                }else if (imageId!=-1){
                    action.withMedia(new UMImage(ShareActivity.this,imageId));
                }
                action.share();

                popupWindow.dismiss();
                return;

            }
        });

    }

     private void setSina(ShareAction action){
         if (text!=null){
             action.withText(text);
         }

         if (targetUrl!=null){
             UMWeb mWeb=new UMWeb(targetUrl);
             action.withMedia(mWeb);
         }else if (videoUrl!=null){
             UMVideo video=new UMVideo(videoUrl);
             if (videoThumb!=null){
                 video.setThumb(new UMImage(ShareActivity.this,videoThumb));
             }

             action.withMedia(video);
         } else if (musicUrl!=null){
             UMusic music=new UMusic(musicUrl);
             if (musicThumb!=null){
                 music.setThumb(new UMImage(ShareActivity.this,musicThumb));
             }
             action.withMedia(music);
         }else  if (imageUrl!=null){
             action.withMedia(new UMImage(ShareActivity.this,imageUrl));
         }else if (imageId!=-1){
             action.withMedia(new UMImage(ShareActivity.this,imageId));
         }

     }
     private void setQQ(ShareAction action){
         if (text!=null){
             action.withText(text);
         }

         if (targetUrl!=null){
             UMWeb mWeb=new UMWeb(targetUrl);
             if (title!=null)
             mWeb.setTitle(title);
             if (text!=null)
             mWeb.setDescription(text);
             if (imageUrl!=null){
                 UMImage thumb=new UMImage(ShareActivity.this,imageUrl);
                 thumb.compressFormat= Bitmap.CompressFormat.PNG;
                 mWeb.setThumb(thumb);
             }else if (imageId!=-1){
                 UMImage thumb=new UMImage(ShareActivity.this,imageId);
                 thumb.compressFormat= Bitmap.CompressFormat.PNG;
                 mWeb.setThumb(thumb);
             }

             action.withMedia(mWeb);
         }else if (videoUrl!=null) {
             UMVideo video = new UMVideo(videoUrl);
             if (title!=null)
                 video.setTitle(title);
             if (text!=null)
                 video.setDescription(text);
             if (videoThumb != null) {
                 video.setThumb(new UMImage(ShareActivity.this, videoThumb));
             }else if (imageUrl!=null){
                 video.setThumb(new UMImage(ShareActivity.this, imageUrl));

             }
             action.withMedia(video);
         }else if (musicUrl!=null){
             UMusic music=new UMusic(musicUrl);
             if (musicThumb!=null){
                 music.setThumb(new UMImage(ShareActivity.this,musicThumb));
             }
             action.withMedia(music);
         }else  if (imageUrl!=null){
             action.withMedia(new UMImage(ShareActivity.this,imageUrl));
         }else if (imageId!=-1){
             action.withMedia(new UMImage(ShareActivity.this,imageId));
         }

     }
    public void showPop() {
        //设置popwindow显示位置

        hadShow=true;
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //获取popwindow焦点
        popupWindow.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popupWindow.setOutsideTouchable(true);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {

                ShareActivity.this.finish();
                overridePendingTransition(R.anim.umeng_socialize_fade_out, R.anim.umeng_socialize_fade_out);
            }
        });
        popupWindow.update();
        popupWindow.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM,0,0);
        findViewById(R.id.main_lay).setBackgroundColor(getResources().getColor(R.color.gray_transparent));
    }
     private UMShareListener umShareListener = new UMShareListener() {
         @Override
         public void onStart(SHARE_MEDIA platform) {
             if (UmShare.mListener!=null){
                 UmShare.mListener.onStart(platform.toString());
             }
         }

         @Override
         public void onResult(SHARE_MEDIA platform) {
//             Toast.makeText(ShareActivity.this,"分享成功", Toast.LENGTH_SHORT).show();
             Log.d("plat","platform"+platform);
            if (UmShare.mListener!=null){
                UmShare.mListener.onSuccess(platform.toString());
            }
         }

         @Override
         public void onError(SHARE_MEDIA platform, Throwable t) {
//             Toast.makeText(ShareActivity.this,"分享失败", Toast.LENGTH_SHORT).show();

             if (UmShare.mListener!=null){
                 UmShare.mListener.onError(platform.toString(),t);
             }
         }

         @Override
         public void onCancel(SHARE_MEDIA platform) {
//             Toast.makeText(ShareActivity.this,"分享取消", Toast.LENGTH_SHORT).show();
             if (UmShare.mListener!=null){
                 UmShare.mListener.onCancel(platform.toString());
             }
         }
     };

}
